﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AcademiaInteligenteWeb
{
    public partial class NewPlanoExercicio : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int idPlanoExercicio;
            ddlMaquina.DataSource = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().ListarMaquina();
            ddlMaquina.DataTextField = "descricao";
            ddlMaquina.DataValueField = "id";
            ddlMaquina.DataBind();

            ddlExercicio.DataSource = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().ListarExercicio();
            ddlExercicio.DataTextField = "descricao";
            ddlExercicio.DataValueField = "id";
            ddlExercicio.DataBind();

            ddlModalidade.DataSource = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().SelecionarModalidadePorAcademia(Convert.ToInt32(Session[Util.SESSION_ACADEMIA_ID]));
            ddlModalidade.DataTextField = "descricao";
            ddlModalidade.DataValueField = "id";
            ddlModalidade.DataBind();
            if (!IsPostBack)
            {
                if (int.TryParse(Request["id"], out idPlanoExercicio))
                {
                    WsAcademiaInteligente.PlanoExercicio planoExercicio = new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().SelecionarPlanoExercicioPorID(idPlanoExercicio);
                    ddlExercicio.SelectedValue = planoExercicio.id_exercicio.ToString();
                    ddlMaquina.SelectedValue = planoExercicio.id_maquina.ToString();
                    ddlModalidade.SelectedValue = planoExercicio.id_modalidade.ToString();
                    txtSerie.Text = planoExercicio.serie.ToString();
                    txtTempo.Text = new DateTime().AddHours(planoExercicio.tempo).ToString("HH:mm");
                    txtQuantidade.Text = planoExercicio.ToString();
                }

            }

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            int idPlanoExercicio;
            WsAcademiaInteligente.PlanoExercicio planoExercicio = new WsAcademiaInteligente.PlanoExercicio();
            planoExercicio.id_cliente = Convert.ToInt32(Request["id_cliente"]);
            planoExercicio.id_academia = Convert.ToInt32(Request["id_academia"]);
            planoExercicio.id_modalidade = Convert.ToInt32(ddlModalidade.SelectedValue);
            planoExercicio.id_exercicio = Convert.ToInt32(ddlExercicio.SelectedValue);
            planoExercicio.id_maquina = Convert.ToInt32(ddlMaquina.SelectedValue);
            planoExercicio.serie = Convert.ToInt32(txtSerie.Text);
            planoExercicio.quantidade = Convert.ToInt32(txtQuantidade.Text);
            planoExercicio.tempo = Convert.ToInt32(txtTempo.Text.Substring(0, 2)) + Convert.ToInt32(txtTempo.Text.Substring(3, 2)) / 60;
            if (int.TryParse(Request["id"], out idPlanoExercicio))
            {
                planoExercicio.id = idPlanoExercicio;
                new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().AlterarPlanoExercicio(planoExercicio);
            }
            else
            {
                new WsAcademiaInteligente.WsAcademiaInteligenteSoapClient().InserirPlanoExercicio(planoExercicio);
            }
        }
    }
}